package com.example.administrator.funchat.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

import com.example.administrator.funchat.R;
import com.example.administrator.funchat.fragment.LoginFragment;
import com.example.administrator.funchat.model.User;

/**
 * Created by Administrator on 2016/7/2.
 */
public class MyDialog extends Activity implements View.OnClickListener {

    private EditText minName;
    private EditText xinQing;
    private RadioButton buttonMan;
    private RadioButton buttonWoman;
    private EditText address;
    private Button okButton;
    private String info;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.myself_dialog);

        minName = (EditText) findViewById(R.id.min_name_edit);
        xinQing = (EditText) findViewById(R.id.xinqing_edit);
        buttonMan = (RadioButton) findViewById(R.id.radiobutton1);
        buttonWoman = (RadioButton) findViewById(R.id.radiobutton2);
        address = (EditText) findViewById(R.id.address_edit);
        okButton = (Button) findViewById(R.id.ok);

        okButton.setOnClickListener(this);
        setInfo(MainActivity.u);//设置信息

    }

    //设置信息
    public void setInfo(User u) {
        if (u.getName() != null && !u.getName().equals("")) {
            minName.setText(u.getName());
        }
        if (u.getSignature() != null && !u.getSignature().equals("")) {
            xinQing.setText(u.getSignature());
        }

        if (u.getSex().equals("男")) {
            buttonMan.setChecked(true);
        } else if (u.getSex().equals("女")) {
            buttonWoman.setChecked(true);
        } else {

        }
        if (u.getAddress() != null && !u.getAddress().equals("")) {
            address.setText(u.getAddress());
        }

    }

    //重写返回键
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getRepeatCount() == 0) {

            Bundle bundle02 = new Bundle();
            Intent intent02 = new Intent();
            bundle02.putString("nickname", "");
            bundle02.putString("mood", "");
            bundle02.putString("sex", "");
            bundle02.putString("address", "");
            intent02.putExtras(bundle02);
            MyDialog.this.setResult(10, intent02);
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    //点击事件监听
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ok:
                Intent intent = new Intent();
                User u = MainActivity.u;
                u.setName(minName.getText().toString());
                u.setSignature(xinQing.getText().toString());
                if (buttonMan.isChecked()) {
                    u.setSex("男");
                } else if (buttonWoman.isChecked()) {
                    u.setSex("女");
                } else {
                    u.setSex("未知");
                }
                u.setAddress(address.getText().toString());
                intent.putExtra("u", u);
                MyDialog.this.setResult(10, intent);
                finish();
                break;
            default:
                break;
        }


    }
}
